package com.link2room.quintesson.taobaoController;

import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.QuintessonSysExceptionEnum;
import com.link2room.jetfire.dto.result.base.ReservationDto;
import com.link2room.quintesson.dto.param.third.TaobaoThirdParamDto;
import com.link2room.quintesson.exception.TaobaoException;
import com.link2room.jetfire.constant.ReservationConstant;
import com.link2room.quintesson.facade.ITaobaoReservationFacadeService;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by sss on 2017/5/3.
 */
@RestController
@RequestMapping("/taobao/Reservations")
public class TaobaoReservationController {
    @Resource
    private ITaobaoReservationFacadeService taobaoReservationFacadeService;

    @RequestMapping(value = "/taobaoService")
    public void taobaoService(@RequestBody String body, HttpServletResponse response) throws Exception{

        TaobaoThirdParamDto taobaoThirdParamDto =new TaobaoThirdParamDto();
        taobaoThirdParamDto.setBody(body);
        String xml=this.excuteTaobaoReservation(taobaoThirdParamDto);

        response.setContentType("text/xml;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(xml);
        response.getWriter().close();
        response.getWriter().flush();
    }

    private String excuteTaobaoReservation(TaobaoThirdParamDto taobaoThirdParamDto)throws Exception{
        Document xml = DocumentHelper.parseText(taobaoThirdParamDto.getBody());
        Element rootElement = xml.getRootElement();
        String rootElementName=rootElement.getName();

        Document doc = DocumentHelper.createDocument();
        Element result = doc.addElement("Result");
        Element resultCode=result.addElement("ResultCode").addText("0");
        Element message=result.addElement("Message");
        //下单请求处理
        if("ValidateRQ".equals(rootElementName)){
            try {
                String inventoryPrice=taobaoReservationFacadeService.resolveTaobaoValidateRQ(rootElement);
                result.addElement("InventoryPrice").addText(inventoryPrice);
            }catch (Exception e){
                if(e instanceof TaobaoException){
                    TaobaoException taobaoException = (TaobaoException) e;
                    if(taobaoException.getCode()== QuintessonSysExceptionEnum.TAOBAO_ROOM_FULL.getCode()){
                        resultCode.setText("-1");
                        message.setText(QuintessonSysExceptionEnum.TAOBAO_ROOM_FULL.getMsg());
                    }else if(taobaoException.getCode()==QuintessonSysExceptionEnum.TAOBAO_ROOM_LESS.getCode()){
                        resultCode.setText("-3");
                        message.setText(QuintessonSysExceptionEnum.TAOBAO_ROOM_LESS.getMsg());
                    }else {
                        resultCode.setText("-4");
                        message.setText(e.getMessage());
                    }
                }else {
                    resultCode.setText("-4");
                    message.setText(e.getMessage());
                }
            }
        }else if("BookRQ".equals(rootElementName)){
            try {
                String l2rNo=taobaoReservationFacadeService.resolveTaobaoBookRQ(rootElement);
                message.setText("接收到去啊订单");
                result.addElement("OrderId").setText(l2rNo);
            }catch (Exception e){
                resultCode.setText("-102");
                message.setText(e.getMessage());
            }
        }else if("CancelRQ".equals(rootElementName)){
            try {
                taobaoReservationFacadeService.resolveTaobaoCanceleRQ(rootElement);
                resultCode.setText("-100");
                message.setText("");
                result.addElement("OrderId").setText("");
            }catch (Exception e){
                resultCode.setText("-200");
                message.setText(e.getMessage());
            }
        }else if("QueryStatusRQ".equals(rootElementName)){
            try {
                ReservationDto reservation = taobaoReservationFacadeService.resolveTaobaoQueryRQ(rootElement);
                message.addText("处理成功");
                result.addElement("TaoBaoOrderId").addText(reservation.getReservationBaseDto().getTerminalNo());
                result.addElement("OrderId").addText(reservation.getReservationBaseDto().getL2RNo());
                if(StringUtil.isNotBlank(reservation.getReservationBaseDto().getCrsNo())){
                    result.addElement("PmsResID").addText(reservation.getReservationBaseDto().getCrsNo());
                }
                String status;
                if(ReservationConstant.CONFIRMSTA.AUTOCFMD.equals(reservation.getReservationBaseDto().getConfirmSta())){
                    status="1";
                }else if(ReservationConstant.CONFIRMSTA.NOTCFMD.equals(reservation.getReservationBaseDto().getConfirmSta())){
                    status="3";
                }else if(ReservationConstant.CONFIRMSTA.CANCELCFMD.equals(reservation.getReservationBaseDto().getConfirmSta())){
                    status="4";
                }else {
                    status="2";
                }
                result.addElement("Status").setText(status);
            }catch (Exception e){
                resultCode.setText("-301");
                message.setText(e.getMessage());
            }
        }
        return doc.asXML();
    }
}
